//
//  HMSearchView.swift
//  MyWeibo
//
//  Created by apple on 16/4/16.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class HMSearchView: UIView {
    
    @IBOutlet weak var tx_textField: UITextField!
    @IBOutlet weak var tx_textFieldRightCons: NSLayoutConstraint!
    @IBOutlet weak var btn_cancelButton: UIButton!
    
    //类方法加载xib
    class func searchView() -> HMSearchView {
        
        return NSBundle.mainBundle().loadNibNamed("SearchView", owner: nil, options: nil).last! as! HMSearchView
        
    }
    
    
    //以代码创建会走的方法
    override init(frame: CGRect) {
        
        super.init(frame: frame)
    }
    
    //xib'中唤醒
    override func awakeFromNib() {
        
        // 设置 leftView
        leftView.frame.size = CGSize(width: self.frame.height, height: self.frame.height)
        tx_textField.leftView = leftView
        tx_textField.leftViewMode = .Always
        
        
    }
    
    
    //加载xib的时候调用
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
    }
    
    //点击编辑的时候调用
    @IBAction func editingDidBegin
        (sender: UITextField) {
            
            //点击编辑框的时候更改约束
            self.tx_textFieldRightCons.constant = self.btn_cancelButton.frame.width
            
            //执行动画
            UIView.animateWithDuration(0.5) { () -> Void in
                
                self.layoutIfNeeded()
            }
            print("开始编辑了")
    }
    //点击取消的时候调用
    @IBAction func didClickButton(sender: UIButton) {
        
        //击编辑框的时候更改约束
        self.tx_textFieldRightCons.constant = 0
        
        //执行动画
        UIView.animateWithDuration(0.5) { () -> Void in
            
            self.layoutIfNeeded()
        }
    
        self.tx_textField.resignFirstResponder()
        
    }
    
    
    //懒加载控件
    
    private lazy var leftView: UIImageView = {
        
        let imageView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        
        //设置内容模式
        imageView.contentMode = .Center
        
        return imageView
    }()
    
}
